<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>闭包的好处</title>
</head>

<body>

</body>


<script>
    // 闭包的好处:
    // 防止全局变量的污染。
    // 让变量常驻内存、不会被垃圾回收机制清除
    // 可以声明私有成员 





    function a() {
        var a = 1;
        var s = function b() {
            a++;
            alert(a);
        }
        return s;
    }

    var s = a();



    var test = (function() {
        var sss = 1;

        var aa = function aa() {
            alert(sss++);
        }

        var bb = function bb() {
            alert(sss * 10);
        }

        return {
            aa,
            bb
        }
    })();


    // test.aa();
    // test.aa();


    var ss = function() {
        var s1s = function aa() {
            alert(1);
        }
        var s2s = function bb() {
            alert(1);

        }
        return {
            s1s,
            s2s
        }
    };

    var ll = ss();
    ll.s1s();
</script>

</html>